package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.QuotationChangeMapper;
import com.ruoyi.crm.domain.QuotationChange;
import com.ruoyi.crm.service.IQuotationChangeService;

/**
 * 报价变更单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-27
 */
@Service
public class QuotationChangeServiceImpl implements IQuotationChangeService
{
    @Autowired
    private QuotationChangeMapper quotationChangeMapper;

    /**
     * 查询报价变更单
     *
     * @param id 报价变更单主键
     * @return 报价变更单
     */
    @Override
    public QuotationChange selectQuotationChangeById(String id)
    {
        return quotationChangeMapper.selectQuotationChangeById(id);
    }

    /**
     * 查询报价变更单列表
     *
     * @param quotationChange 报价变更单
     * @return 报价变更单
     */
    @Override
    public List<QuotationChange> selectQuotationChangeList(QuotationChange quotationChange)
    {
        return quotationChangeMapper.selectQuotationChangeList(quotationChange);
    }

    /**
     * 新增报价变更单
     *
     * @param quotationChange 报价变更单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuotationChange(QuotationChange quotationChange)
    {
        int rows = quotationChangeMapper.insertQuotationChange(quotationChange);
        insertCustomerProduct(quotationChange);
        return rows;
    }

    /**
     * 修改报价变更单
     *
     * @param quotationChange 报价变更单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuotationChange(QuotationChange quotationChange)
    {
        quotationChange.setUpdateTime(DateUtils.getNowDate());
        quotationChangeMapper.deleteCustomerProductByCustomerName(quotationChange.getId());
        insertCustomerProduct(quotationChange);
        return quotationChangeMapper.updateQuotationChange(quotationChange);
    }

    /**
     * 批量删除报价变更单
     *
     * @param ids 需要删除的报价变更单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuotationChangeByIds(String[] ids)
    {
        return quotationChangeMapper.deleteQuotationChangeByIds(ids);
    }

    /**
     * 删除报价变更单信息
     *
     * @param id 报价变更单主键
     * @return 结果
     */
    @Override
    public int deleteQuotationChangeById(String id)
    {
        quotationChangeMapper.deleteCustomerProductByCustomerName(id);
        return quotationChangeMapper.deleteQuotationChangeById(id);
    }

    @Override
    public QuotationChange getQuoChangeInfo(String quochangeNo) {
        return quotationChangeMapper.getQuoChangeInfo(quochangeNo);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param quotationChange 报价变更单对象
     */
    public void insertCustomerProduct(QuotationChange quotationChange)
    {
        List<CustomerProduct> customerProductList = quotationChange.getCustomerProductList();
        String id = quotationChange.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                quotationChangeMapper.batchCustomerProduct(list);
            }
        }
    }
}